package com.jgc.stage01.daemon;

/**
 * Created by Administrator on 2022/2/10.
 * 当 JVM 中不存在任何一个正在运行的非守护线程时，则 JVM 进程即会退出
 * 通常来说，守护线程经常被用来执行一些后台任务，但是呢，你又希望在程序退出时，或者说 JVM 退出时，线程能够自动关闭，此时，守护线程是你的首选
 */
public class MyDaemonThread extends Thread {
    public static void main(String[] args) {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        MyThread myThread = new MyThread();
        myDaemonThread.setDaemon(true);
        myDaemonThread.start();
        myThread.start();
    }
    @Override
    public void run() {
        while (true){
            System.out.println("守护进程");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class MyThread extends Thread{
    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println("非daemon进程");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}